Page({
  data: {
    userInfo: {
      avatarUrl: '',
      nickName: ''
    },
    joinDays: 32,
    showGuide: true,
    
    // 作者相关
    authorList: ['李白', '杜甫', '王维'],
    showAuthorPopup: false,
    authorInput: '',
    recommendedAuthors: ['苏轼', '白居易', '辛弃疾', '陆游'],
    
    // 题材相关
    themeList: ['山水', '思乡', '边塞'],
    showThemePopup: false,
    themeInput: '',
    recommendedThemes: ['爱情', '田园', '送别', '咏物'],
    
    // 统计数据
    stats: {
      poems: 12,
      authors: 5,
      days: 7
    }
  },

  onLoad() {
    // 获取用户信息
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      this.setData({
        userInfo: userInfo
      });
    }
    
    // 计算加入天数
    const joinDate = wx.getStorageSync('joinDate');
    if (joinDate) {
      const days = this.calculateDays(new Date(joinDate), new Date());
      this.setData({
        joinDays: days
      });
    } else {
      // 首次使用，记录加入日期
      wx.setStorageSync('joinDate', new Date().toString());
    }
  },
  
  // 计算天数差
  calculateDays(startDate: Date, endDate: Date): number {
    const diffTime = Math.abs(endDate.getTime() - startDate.getTime());
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    return diffDays;
  },
  
  // 隐藏首次使用引导
  hideFirstTimeGuide() {
    this.setData({
      showGuide: false,
      showAuthorPopup: true
    });
  },
  
  // 显示作者弹出层
  showAuthorPopup() {
    this.setData({
      showAuthorPopup: true,
      showThemePopup: false
    });
  },
  
  // 显示题材弹出层
  showThemePopup() {
    this.setData({
      showThemePopup: true,
      showAuthorPopup: false
    });
  },
  
  // 隐藏所有弹出层
  hidePopup() {
    this.setData({
      showAuthorPopup: false,
      showThemePopup: false
    });
  },
  
  // 处理作者输入
  onAuthorInput(e: WechatMiniprogram.Input) {
    this.setData({
      authorInput: e.detail.value
    });
  },
  
  // 处理题材输入
  onThemeInput(e: WechatMiniprogram.Input) {
    this.setData({
      themeInput: e.detail.value
    });
  },
  
  // 添加作者
  addAuthor(e: WechatMiniprogram.TouchEvent) {
    const name = e.currentTarget.dataset.name as string;
    if (name && !this.data.authorList.includes(name)) {
      const updatedList = [...this.data.authorList, name];
      this.setData({
        authorList: updatedList,
        showAuthorPopup: false,
        authorInput: ''
      });
    }
  },
  
  // 添加自定义作者
  addCustomAuthor() {
    const name = this.data.authorInput.trim();
    if (name && !this.data.authorList.includes(name)) {
      const updatedList = [...this.data.authorList, name];
      this.setData({
        authorList: updatedList,
        showAuthorPopup: false,
        authorInput: ''
      });
    } else if (!name) {
      wx.showToast({
        title: '请输入作者名称',
        icon: 'none'
      });
    }
  },
  
  // 添加题材
  addTheme(e: WechatMiniprogram.TouchEvent) {
    const name = e.currentTarget.dataset.name as string;
    if (name && !this.data.themeList.includes(name)) {
      const updatedList = [...this.data.themeList, name];
      this.setData({
        themeList: updatedList,
        showThemePopup: false,
        themeInput: ''
      });
    }
  },
  
  // 添加自定义题材
  addCustomTheme() {
    const name = this.data.themeInput.trim();
    if (name && !this.data.themeList.includes(name)) {
      const updatedList = [...this.data.themeList, name];
      this.setData({
        themeList: updatedList,
        showThemePopup: false,
        themeInput: ''
      });
    } else if (!name) {
      wx.showToast({
        title: '请输入题材名称',
        icon: 'none'
      });
    }
  },
  
  // 导航相关方法
  navigateTo(e: WechatMiniprogram.TouchEvent) {
    const page = e.currentTarget.dataset.page as string;
    wx.switchTab({
      url: `../${page}/${page}`
    });
  }
}); 